import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import classification_report, accuracy_score
import matplotlib.pyplot as plt
from sklearn.tree import plot_tree
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.model_selection import GridSearchCV

# 读数据
df_data = pd.read_csv("train.csv")
# df_data.info()

# 对数据进行预处理
x = df_data[['Pclass', 'Sex', 'Age']].copy()
y = df_data['Survived']

# 对age缺失值进行处理
x['Age'] = x['Age'].fillna(x['Age'].mean())

# 热编码
x = pd.get_dummies(x)
# print(x)

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=24)

# 特征工程 略


# 场景1：构建单一决策树 : DecisionTreeClassifier
print(x)

# 准确分：accuracy_score


# 梯度提升树对象(GBDT) ： GradientBoostingClassifier
model = GradientBoostingClassifier()
model.fit(x_train, y_train)
y_pre_1 = model.predict(x_test)
print(f'准确分:{accuracy_score(y_test, y_pre_1)}')

# 场景3： 针对GBDT 模型进行参数调优： param_dict={}
model_2 = GradientBoostingClassifier()
# 交叉验证＋网格搜索 GridSearchCV
# pra_dict = {'learning_rate': [0.01], 'n_estimators': [50, 60], 'max_depth': [3, 5]}
param = {'n_estimators':[1, 3, 5, 10], 'max_depth': [1, 3, 5, 7], 'learning_rate':[0.1, 0.2, 0.3]}
cv = GridSearchCV(estimator=model_2, param_grid=param, cv=4)
cv.fit(x_train, y_train)

# 最优评分和模型
print(cv.best_estimator_)
y_pre_2 = cv.best_estimator_.predict(x_test)
print(f'准确分:{accuracy_score(y_test, y_pre_2)}')